package asteroids.model;

import java.util.Random;
import asteroids.Util;
import be.kuleuven.cs.som.annotate.*;

/**
 * A class representing an Asteroid.
 * @author Martin & Anton
 *
 */
public class Asteroid extends SpaceObject {
	private static final double density = 2.65E12;
	private Random random;

	/**
	 * 
	 * @param x
	 * @param y
	 * @param speed_x
	 * @param speed_y
	 * @param radius
	 * @throws IllegalArgumentException
	 */
	public Asteroid(double x, double y, double speed_x, double speed_y,double radius) throws IllegalArgumentException {
		this(x, y, speed_x, speed_y, radius, c, new Random());
	}
	/**
	 * 
	 * @param x
	 * @param y
	 * @param speed_x
	 * @param speed_y
	 * @param radius
	 * @param random
	 * @throws IllegalArgumentException
	 */
	public Asteroid(double x, double y, double speed_x, double speed_y,double radius, Random random) throws IllegalArgumentException {
		this(x, y, speed_x, speed_y, radius, c, random);
	}
	
	/**
	 * 
	 * @param x
	 * @param y
	 * @param speed_x
	 * @param speed_y
	 * @param radius
	 * @param max_speed
	 * @param random
	 * @throws IllegalArgumentException
	 */
	public Asteroid(double x, double y, double speed_x, double speed_y,double radius, double max_speed, Random random)	throws IllegalArgumentException {
		super(x, y, speed_x, speed_y, radius, (4/3)*Math.PI*Math.pow(radius, 3)*density, 0, c);
		this.random = random;
	}
	
	/**
	 * This method overrides die() from SpaceObject.
	 * It possibly creates 2 new asteroids.
	 * @effect Two 'child-asteroids' are created, if this is located in a world an has a radius bigger than 30.
	 * 			|if(this.getWorld() != null && Util.fuzzyMoreThanOrEqualTo(this.getRadius(), 30))
	 * 			|then{
	 * 			|Asteroid child1 = new Asteroid(getX()+cos*getRadius()/2, getY()+sin*getRadius()/2, 
	 *			|						1.5*cos*this.getSpeed().getTotalSpeed(), 1.5*sin*this.getSpeed().getTotalSpeed(),
	 *			|						getRadius()/2, new Random());
	 *			|Asteroid child2 = new Asteroid(getX()-cos*getRadius()/2, getY()-sin*getRadius()/2, 
	 *			|						-1.5*cos*this.getSpeed().getTotalSpeed(), -1.5*sin*this.getSpeed().getTotalSpeed(),
	 *			|						getRadius()/2, new Random());
	 *			|this.getWorld().addSpaceObject(child1);
	 *			|this.getWorld().addSpaceObject(child2);
	 */
	@Override
	public void die(){
		if(getStatus() != Status.DEAD) {
			if(this.getWorld() != null && Util.fuzzyMoreThanOrEqualTo(this.getRadius(), 30)){
				double direction = random.nextDouble();
				double cos = Math.cos(direction); 
				double sin = Math.sin(direction);
				
				Asteroid child1 = new Asteroid(getX()+cos*getRadius()/2, getY()+sin*getRadius()/2, 
										1.5*cos*this.getSpeed().getTotalSpeed(), 1.5*sin*this.getSpeed().getTotalSpeed(),
										getRadius()/2, new Random());
				Asteroid child2 = new Asteroid(getX()-cos*getRadius()/2, getY()-sin*getRadius()/2, 
										-1.5*cos*this.getSpeed().getTotalSpeed(), -1.5*sin*this.getSpeed().getTotalSpeed(),
										getRadius()/2, new Random());
				this.getWorld().addSpaceObject(child1);
				this.getWorld().addSpaceObject(child2);
			}
			super.die();
		}
	}
	
}
